package zflx_new;

import org.apache.log4j.Logger;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

/**
 * @ Auther: o2o-rd-0008
 * @ Date:   2020/7/16 18:59
 * @ Param:  ${PARAM}
 * @ Description:
 */
public class g_tables_op {

    private static Logger logger = Logger.getLogger(g_tables.class);
    public static void main(String[] args) throws IOException, SQLException {
        //获取本得的属性文件
        Properties pro = new Properties();
        InputStream is = g_tables_op.class.getClassLoader().getResourceAsStream("gov/gov_allplat_op.properties");
        pro.load(is);

        String schem = pro.getProperty("schem");
        String year = pro.getProperty("year");
        String month = pro.getProperty("month");
        String timeStamp = pro.getProperty("timeStamp");
        String shopSchema = pro.getProperty("shopSchema");
        String goodSchema = pro.getProperty("goodSchema");
        String midSchema = pro.getProperty("midSchema");

        g_tables_op.g_allshop(year, month, timeStamp, schem,shopSchema,goodSchema,midSchema);

    }

    /**
     * 传参
     *
     * @param year
     * @param month
     * @param timeStamp
     * @param schem
     */
    public static void g_allshop(String year, String month, String timeStamp, String schem, String shopSchema, String goodSchema, String midSchema) throws SQLException {

/**
 * 考拉
 * 寺库
 * 聚美优品
 * 蜜芽
 * 贝贝
 * 云集
 * 当当
 * 国美
 */
// "taobao|9", "tmall|10", "jd|5", "suning|3", "meituan|16", "elm|13", "dazhongdp|24"
//                        , "meituan_tg|23", "pinduoduo|20", "kaola|4", "secoo|19", "jumei|6", "miya|11", "beibei|58", "shunfeng|15", "yunji|29",
//                        "dangdang|8", "guomei|2"
//                        , "hmxs|99"

//        "taobao|9", "tmall|10", "jd|5", "suning|3", "meituan|16", "elm|13", "dazhongdp|24"
//                , "meituan_tg|23", "pinduoduo|20", "kaola|4", "jumei|6", "miya|11", "beibei|58",  "yunji|29",
//                "guomei|2", "hmxs|99"        
//        Connection conn = g_tables.GetConnection("dbadmin", "Goal@Data@2018");
        Connection conn = g_tables_op.GetConnection("sysadmin", "Bigdata@123");

        String[] platname_platid = {"create|123","taobao|9", "tmall|10", "jd|5", "suning|3", "meituan|16", "elm|13", "dazhongdp|24"
                , "meituan_tg|23", "pinduoduo|20", "kaola|4", "secoo|19", "jumei|6", "miya|11", "beibei|58",  "yunji|29",
                "dangdang|8","guomei|2", "hmxs|99"};

        /* 开始遍历 */
        for (String plat : platname_platid) {
            logger.info("[platformName:" + plat.split("\\|")[0] + "]");
            logger.info("[platformId:" + plat.split("\\|")[1] + "]");
            if(!plat.split("\\|")[0].equals("create")){
                System.out.println(plat + ": =============================================");
                g_tables_op.allplat(plat.split("\\|")[1], conn, plat.split("\\|")[0], year, month, timeStamp, schem,shopSchema,goodSchema);
            }
        }
        /**
         * 遍历插入企业个人表
         */
        for (String plat : platname_platid) {
            logger.info("[platformName:" + plat.split("\\|")[0] + "]");
            logger.info("[platformId:" + plat.split("\\|")[1] + "]");
            System.out.print(plat + ": =============================================");
            g_tables_op.qiyeorperson(plat.split("\\|")[1], conn, plat.split("\\|")[0], year, month, timeStamp, schem,midSchema);
        }
        //更新跨境表
        g_tables_op.updateoversea(conn,schem,year);

        //更新工商信息表
        g_tables_op.updateStandCompany(conn,schem);
        conn.close();
    }




    /**
     * 表名 调用
     *
     * @param platformId
     * @param conn
     * @param platformName
     * @param year
     * @param month
     * @param timeStamp
     * @param schem
     */
    public static void allplat(String platformId, Connection conn, String platformName, String year, String month, String timeStamp, String schem, String shopSchema, String goodSchema) {
        System.out.println("===================基本店铺表=========================================================");
        logger.info("[table:o2o_allPlat_shop_BDL]");
        if(platformId.equals("5") | platformId.equals("10") | platformId.equals("3") | platformId.equals("16") | platformId.equals("13")
                | platformId.equals("24") | platformId.equals("23")|platformId.equals("9") | platformId.equals("20") | platformId.equals("99")){
            g_Sqlutils_op.o2o_allplat_shop_BDL_insert(conn,
                                                    "" + schem + "", "o2o_allPlat_shop_BDL_" + year + "",
                                                    ""+shopSchema+"", "g_" + year + "_" + platformName + "_shop_" + month + "",
                                                    "" +platformId+ "",
                                                    "" +platformName+ "",
                                                    "" +timeStamp+ ""
                                                    );
        }

        System.out.println("===================公司信息表=========================================================");
        logger.info("[table:o2o_stand_shopCompany_BDL]");
        if (platformId.equals("9") | platformId.equals("10") | platformId.equals("5") | platformId.equals("3")) {
            //总统计淘宝，天猫，京东，苏宁公司信息
            g_Sqlutils_op.o2o_allPlat_shop_BDL_company_insert(conn,
                    "" + schem + "", "o2o_stand_shopCompany_BDL_" + year + "",
                    ""+shopSchema+"", "g_" + year + "_" + platformName + "_shop_" + month + "",
                    "" +platformId+ "",
                    "" +platformName+ "",
                    "" +timeStamp+ "");
        }

        System.out.println("===================畅销店铺表=========================================================");
        logger.info("[table:o2o_bestsell_shop_APP]");
        if (platformId.equals("5") | platformId.equals("10") | platformId.equals("24") | platformId.equals("16") | platformId.equals("13")
                | platformId.equals("3") | platformId.equals("20") | platformId.equals("23") | platformId.equals("9") | platformId.equals("99")) {
            g_Sqlutils_op.o2o_bestsell_shop_APP_insert(conn,
                                                    "" + schem + "", "o2o_bestsell_shop_APP_" + year + "",
                                                    "" + schem + "", "o2o_allPlat_shop_BDL_" + year + "",
                                                    "" +platformId+ "",
                                                    "" +platformName+ "",
                                                    "" +timeStamp+ ""
            );
        }

        System.out.println("===================人气店铺表=========================================================");
        logger.info("[table:o2o_sentiment_shop_APP]");
        if (platformId.equals("5") | platformId.equals("10") | platformId.equals("24") | platformId.equals("16")
                | platformId.equals("13") | platformId.equals("23") | platformId.equals("9")) {
            g_Sqlutils_op.o2o_sentiment_shop_APP_insert(conn,
                    "" + schem + "", "o2o_sentiment_shop_APP_" + year + "",
                    "" + schem + "", "o2o_allPlat_shop_BDL_" + year + "",
                    "" +platformId+ "",
                    "" +platformName+ "",
                    "" +timeStamp+ ""
            );
        }

        System.out.println("===================农产品畅销单品表=========================================================");
        logger.info("[table:o2o_bestsell_product_APP]");
        if(platformId.equals("9") | platformId.equals("10") | platformId.equals("5") | platformId.equals("3")
                | platformId.equals("20")){
            g_Sqlutils_op.o2o_bestsell_product_APP_create(conn,
                    ""+goodSchema+"", "g_" + year + "_" + platformName + "_" + month + "",
                    "" + platformId + "",
                    "" + platformName + "",
                    "" + timeStamp + "");
            g_Sqlutils_op.o2o_bestsell_product_APP_insert(conn,
                    "" + schem + "", "o2o_bestsell_product_APP_" + year + "",
                    "" + platformId + "",
                    "" + platformName + "",
                    "" + timeStamp + "");

        }

        System.out.println("=====================全网畅销单品表=======================================================");
        logger.info("[table:o2o_bestsell_APP]");
        if(platformId.equals("9") | platformId.equals("10") | platformId.equals("5") | platformId.equals("3")
                | platformId.equals("20")| platformId.equals("16")| platformId.equals("13")| platformId.equals("24")
                | platformId.equals("23")){
            g_Sqlutils_op.o2o_bestsell_APP_insert(conn,
                    "" + schem + "", "o2o_bestsell_APP_" + year + "",
                    ""+goodSchema+"", "g_" + year + "_" + platformName + "_" + month + "",
                    "" + platformId + "",
                    "" + platformName + "",
                    "" + timeStamp + "");

        }




        System.out.println("=====================农产品人气单品表=======================================================");
        logger.info("[table:o2o_sentiment_product_APP]");
        if (platformId.equals("9") || platformId.equals("10")) {
            g_Sqlutils_op.o2o_sentiment_product_APP_insert(conn,
                    "" + schem + "", "o2o_sentiment_product_APP_" + year + "",
                    "" + platformId + "",
                    "" + platformName + "",
                    timeStamp
            );
        }

        System.out.println("=====================价格段表=======================================================");
        logger.info("[table:o2o_products_price_APP_2019]");
        if (platformId.equals("3")|platformId.equals("5")|platformId.equals("9")|platformId.equals("10")|platformId.equals("20")) {
            g_Sqlutils_op.o2o_products_price_APP_insert(conn,
                    ""+schem+"", "o2o_products_price_APP_"+year+"",
                    "" + platformId + "",
                    "" + platformName + "",
                    timeStamp);
        }

        System.out.println("=====================跨境大屏表=======================================================");
        logger.info("[table:o2o_screen_ovresea_app]");
        if (platformId.equals("3")|platformId.equals("5")|platformId.equals("9")|platformId.equals("10")|platformId.equals("20")) {
            
            g_Sqlutils_op.o2o_screen_ovresea_app_insert(conn,
                    "" + schem + "", "o2o_screen_ovresea_app_" + year + "",
                    "" + platformId + "",
                    "" + platformName + "",
                    timeStamp);
            
        }else if (platformId.equals("4") | platformId.equals("19") | platformId.equals("6")| platformId.equals("11")| platformId.equals("58") |platformId.equals("15")
                |platformId.equals("29")|platformId.equals("8")|platformId.equals("2")){
            
            g_Sqlutils_op.o2o_screen_ovresea_app_create(conn,
                    ""+goodSchema+"", "g_" + year + "_" + platformName + "_" + month + "",
                    "" + platformId + "",
                    "" + platformName + "",
                    timeStamp);
            
            g_Sqlutils_op.o2o_screen_ovresea_app_insert(conn,
                    "" + schem + "", "o2o_screen_ovresea_app_" + year + "",
                    "" + platformId + "",
                    "" + platformName + "",
                    timeStamp);
            
        }
    }


    /**
     * 企业或个人表
     * @param platformId
     * @param conn
     * @param platformName
     * @param year
     * @param month
     * @param timeStamp
     * @param schem
     */
    public static void qiyeorperson(String platformId, Connection conn, String platformName, String year, String month, String timeStamp, String schem, String midSchema){
        System.out.println("===================个人企业表=========================================================");

        if("create".equals(platformName)){
            g_Sqlutils_op.o2o_allPlat_shop_BDL_company_or_person_create(conn,
                    ""+midSchema+"", "o2o_allPlat_shop_BDL_2020",
                    ""+schem+"","o2o_allPlat_shop_BDL_"+year+"",
                    "" + platformId + "",
                    "" + platformName + "",
                    timeStamp);
        }else if(platformId.equals("9") || platformId.equals("3") || platformId.equals("10") || platformId.equals("16")
                || platformId.equals("13") || platformId.equals("24") || platformId.equals("23") || platformId.equals("99")
                || platformId.equals("5") || platformId.equals("20")){
            g_Sqlutils_op.o2o_allPlat_shop_BDL_company_or_person_insert(conn,
                    ""+midSchema+"", "o2o_allPlat_shop_BDL_2020",
                    ""+schem+"","o2o_allPlat_shop_BDL_"+year+"",
                    ""+schem+"", "o2o_stand_shopCompany_BDL_"+year+"",
                    "" + platformId + "",
                    "" + platformName + "",
                    timeStamp);
        }

    }

    public static void updateoversea(Connection conn,
                                     String schem, String year) {
        g_Sqlutils_op.is_oversea_plat_update(conn,
                "" + schem + "", "o2o_screen_ovresea_app_" + year + ""
        );
    }

    public static void updateStandCompany(Connection conn,
                                     String schem) {
        g_Sqlutils_op.stand_company_insert(conn,
                "" + schem + "", "o2o_stand_company_IDL"
        );
    }

    /**
     * 数据库连接
     *
     * @param username
     * @param passwd
     * @return
     */
    public static Connection GetConnection(String username, String passwd) {
        String driver = "org.postgresql.Driver";
        String sourceURL = "jdbc:postgresql://192.168.2.234:8000/postgres";
        Connection conn = null;
        try {
            //加载数据库驱动。
            Class.forName(driver).newInstance();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

        try {
            //创建数据库连接。
            conn = DriverManager.getConnection(sourceURL, username, passwd);
            logger.info("Connection succeed!");
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return conn;
    }
    
}
